#coding=utf-8
from sqlalchemy import Column, Integer, UnicodeText, Unicode, ForeignKey
from sqlalchemy.orm import relationship
from ext.model import DBSession, DeclarativeBase
from ext.model_base import DynamicMixin
from ext.model_tree import BranchMixin

__all__ = ['BdKeyword', 'BdDomain', 'BdKeywordRank']

class BdKeyword(DeclarativeBase, DynamicMixin, BranchMixin):
    __tablename__ = 'bd_keyword'
    __branch__ = {'active':['Keywords', 'active', 'active_order']}

class BdDomain(DeclarativeBase, DynamicMixin, BranchMixin):
    __tablename__ = 'bd_domain'
    __branch__ = {'active':['Domains', 'active', 'active_order']}

class BdKeywordRank(DeclarativeBase, DynamicMixin):
    __tablename__ = 'bd_keyword_rank'

    keyword_id = Column(Integer, ForeignKey(BdKeyword.id))
    keyword = relationship('BdKeyword', primaryjoin=keyword_id == BdKeyword.id)
    domain_id = Column(Integer, ForeignKey(BdDomain.id))
    domain = relationship('BdDomain', primaryjoin=domain_id == BdDomain.id)
    rank = Column(Integer)
    location = Column(Unicode(20))
    html = Column(UnicodeText)

    @classmethod
    def save(cls, ** kw):
        DBSession.add(cls(** kw))
        DBSession.flush()
